/**
 * 改变获取验证码url，以重新获取url
 * 验证码图片id必须为validateCodeImg
 */
function reloadValidateCode()
{
	validateCodeImg = document.getElementById("validateCodeImg");
	validateCodeImg.src = "/Dragonfly/validateCode.html?" + Math.random();
}

/**
 * 改变获取验证码url，以重新获取url
 * 验证码图片id必须为validateCodeImg
 */
function reloadTwoValidateCode()
{
	validateCodeImg = document.getElementById("validateCodeImg");
	reportPictureValidateCodeImg = document.getElementById("reportPictureValidateCodeImg");
	var url = "/Dragonfly/validateCode.html?" + Math.random();
	validateCodeImg.src = url;
	reportPictureValidateCodeImg.src = url;
}

/**
 * 验证表单提交内容是否符合规范
 * @return 验证成功返回true，失败返回false
 */
function checkRegisterForm()
{
	var email = document.getElementById("email");
	var password = document.getElementById("password");
	var repassword = document.getElementById("repassword");
	var validateCode = document.getElementById("validateCode");
	
	//验证邮件地址是否符合规范
	var emailtest = /^[\w][\w\-\.]+@[\w\-]+(\.[a-z]+){1,2}$/;
	if(!emailtest.test(email.value))
	{
		var emailWarning = document.getElementById("emailWarning");
		emailWarning.innerHTML = "邮箱格式不正确";
		return false;
	}
	
	//验证密码是否符合规范
	var passwordPattern = /^(\w){8,12}$/;
	if(!passwordPattern.test(password.value))
	{
		var passwordWarning = document.getElementById("passwordWarning");
		passwordWarning.innerHTML = "密码不符合要求（8－12位字母、数字、下划线）";
		return false;
	}
	
	//验证两次密码输入是否匹配
	if(password.value != repassword.value)
	{
		var repasswordWarning = document.getElementById("repasswordWarning");
		repasswordWarning.innerHTML = "两次密码输入不匹配";
		return false;
	}
	
	if(validateCode.value == "")
	{
		var validateCodeWarning = document.getElementById("validateCodeWarning");
		validateCodeWarning.innerHTML = "请输入验证码";
		return false;
	}
	
	return true;
}

/**
 * 验证SQL注入
 * @return 验证成功返回true，失败返回false
 */
function sqlInject()
{
	var commentContent = document.getElementById("commentContent");
	var commentContentWarning = document.getElementById("commentContentWarning");
	var validateCode = document.getElementById("validateCode");
	
	//非空验证
	var reIsEmpty=/^\s{0,}$/;
	if(reIsEmpty.test(commentContent.value))
	{	
		commentContentWarning.innerHTML = "请在评论里输入内容";
		return false;
	}
	
	//SQL验证
	var reSQL= /select|update|delete|exec|count|'|"|=|;|>|<|%/i;
	if(reSQL.test(commentContent.value))
	{
		commentContentWarning.innerHTML = "请不要输入关键字或者SQL关键字";
		return false;
	}
	
	//验证码验证
	if(validateCode.value == "")
	{
		var validateCodeWarning = document.getElementById("validateCodeWarning");
		validateCodeWarning.innerHTML = "请输入验证码";
		return false;
	}

	
	return true;
}

function reportPictureVerify()
{
	var email = document.getElementById("email");
	var validateCode = document.getElementById("reportPictureValidateCode");
	
	//验证邮件地址是否符合规范
	var emailtest = /^[\w][\w\-\.]+@[\w\-]+(\.[a-z]+){1,2}$/;
	if(!emailtest.test(email.value))
	{
		var reportPictureEmailWarning = document.getElementById("reportPictureEmailWarning");
		reportPictureEmailWarning.innerHTML = "邮箱格式不正确";
		return false;
	}
	
	if(validateCode.value == "")
	{
		var reportPictureValidateCodeWarning = document.getElementById("reportPictureValidateCodeWarning");
		reportPictureValidateCodeWarning.innerHTML = "请输入验证码";
		return false;
	}
	
	return true;
}

/**
 * 根据span的id清空span中的内容
 * @param spanId 需要清空的span的id
 */
function clearWarning(spanId)
{
	var warningSpan = document.getElementById(spanId);
	warningSpan.innerHTML="";
}

/**
 * 验证用户详细信息
 * @return 如果验证通过，返回true；不通过，返回false
 */
function checkUserInfo()
{
	var userName = document.getElementById("userNameInput");
	if(userName.value == null || userName.value == "")
	{
		var userNameWarning = document.getElementById("userNameWarning");
		userNameWarning.innerHTML = "请输入昵称以便于交流";

		return false;
	}

	var oldPassword = document.getElementById("oldPasswordInput");
	var password = document.getElementById("passwordInput");
	if(password.value != "" && (oldPassword.value == null || oldPassword.value == ""))
	{
		var oldPasswordWarning = document.getElementById("oldPasswordWarning");
		oldPasswordWarning.innerHTML = "请输入原密码";
		return false;
	}

	var passwordPattern = /^(\w){8,12}$/;
	if(password.value != "" && !passwordPattern.test(password.value))
	{
		var passwordWarning = document.getElementById("passwordWarning");
		passwordWarning.innerHTML = "新密码不符合要求";
		return false;
	}

	var repassword = document.getElementById("repasswordInput");
	if(password.value != "" && repassword.value != password.value)
	{
		var repasswordWarning = document.getElementById("repasswordWarning");
		repasswordWarning.innerHTML = "重复密码不匹配";
		return false;
	}

	var age = document.getElementById("ageInput");
	var agePattern = /^[0-9]{1,3}$/;
	if(age.value != "" && !agePattern.test(age.value))
	{
		var ageWarning = document.getElementById("ageWarning");
		ageWarning.innerHTML = "无效的年龄（年龄在0到130之间，如保密，请填0）";
		return false;
	}

	var birthdayPattern = /^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))$/;
	var birthday = document.getElementById("birthdayInput");
	if(birthday.value != "" && !birthdayPattern.test(birthday.value))
	{
		var birthdayWarning = document.getElementById("birthdayWarning");
		birthdayWarning.innerHTML = "生日的格式为yyyy-MM-dd";

		return false;
	}

	var slideSpeed = document.getElementById("slideSpeedInput");
	var slideSpeedPattern = /^[0-9]{1,}$/;
	if((slideSpeed.value != "" && !slideSpeedPattern.test(slideSpeed.value)) 
			|| (slideSpeedPattern.test(slideSpeed.value) && Number(slideSpeed.value) > 10))
	{
		var slideSpeedWarning = document.getElementById("slideSpeedWarning");
		slideSpeedWarning.innerHTML = "幻灯片播放速度为1－10的整数";

		return false;
	}

	return true;
}
